# coding: utf-8
'''
Created on 2009-7-20

@author: roy
'''


import os
import time
import random

from constant import *
from common.httpHepler import *
from common.util import *
#from conf import *
from house.garden import *
from house.ranch import *

from conf import *

class kaixin:
    '''
    kaixin001 wap client
    '''


    def __init__(self):
        '''
        Constructor
        '''
        #self.http = httpHelper()
        #self.http = httpHelper_httplib()
        self.http = httpHelper_urlfetch()
        self.friends = dict()
        self.verify = ""
        self.uid = ""
    
    
    def login(self, userId, verifyCode):
        # xjr555@live.cn
        #self.uid = '14281192'
        #self.verify = '14281192_14281192_1248329381_279f32db0b5f0eb41a03de924128ea2d_kx'
        
        #xjr555@hotmail.com
        #self.uid = '14185677'
        #self.verify = '14185677_14185677_1248330515_35194261c01c249d62b66b2eff9f62cf_kx'
        
        self.uid = userId
        self.verify = verifyCode
        
        return True
#===============================================================================
#        url = '/login/login.php'  
#        
#        content = self.http.login(url, user_data)
#        #print content
#        
#        if content != None:
#            self.verify = content[content.find('verify=') + len('verify='):]
#        else:
#            return False
#       
#        #url = '/home/?' + content
#        url = content
#        content = self.http.read_page(url)
#        
#        list = search_text(r'<a href="/home/\?uid=(\d+?)&amp;verify=([^<]+?)"', content, True)
#        
#        if len(list) > 0:
#            self.uid = list[0][0]
#            #self.verify = list[0][1]
#            return True
#        else:
#            return False
#===============================================================================
        
        
    def getFriends(self):
        start = 0
        self.friends.clear()
        
        self.friends[self.uid] = "mySelf"
        
        while True:
            url = '/friend/?uid=%s&verify=%s&start=%s'
            url = url % (self.uid, self.verify, start)
            
            content = self.http.read_page(url)
            
            if (content == None) or (content == ''):
                break
            
            list = search_text(r"&amp;uid=(\d+?)&amp;([^<]+?)'>([^<]+?)</a>", content, True)
            
            count = len(list)
            if count > 0:
                for i in range(count):
                    #self.friends[list[i][0]] = unicode(list[i][2])
                    self.friends[list[i][0]] = list[i][2]
                start += 10
            else:
                break
        
        #for id, name in self.friends.items():        
        #    log('%s, %s' % (id, name))
        #print len(self.friends)
       
        
    def doGarden(self):
        gd = garden(self.uid, self.http, self.verify)
        gd.harvest()
        
        
    def doRanch(self):
        rh = ranch(self.uid, self.http, self.verify)
        rh.harvest()
        
 
 
def test():
    tt = time.localtime()
    hh = tt[3]
    
    # sleep during 2:00 ~ 5:00 of Asia/Beijing time
    if (hh>17) and (hh<21): 
        return
    
    log('*--- kaixin ---*')
#===============================================================================
#    kx = kaixin()
#    if kx.login() == True:
# #        while True:
#            kx.getFriends()
#            kx.doGarden()
#            kx.doRanch()
# #            time.sleep(120)
#    else:
#        log('login error')
#===============================================================================
 
    guyseed = random.randrange(0, len(conf))
    guy = conf.items()[guyseed]
    
    log('guy:' + guy[0])
 
    kx = kaixin()
    if kx.login(guy[0], guy[1]) == True:
            #kx.getFriends()
            kx.doGarden()
            kx.doRanch()
    else:
        log('login error')        
 
    log('*----- end -----*')
    
    removeOldLog()
 
if __name__ == '__main__':
    test()     

       